package com.guoxin.concurrentprogramming.chapter3.synccontrol;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Administrator
 */
public class ReentrantLockCondition extends Thread {

    private static final ReentrantLock REENTRANT_LOCK = new ReentrantLock();

    private static final Condition CONDITION = REENTRANT_LOCK.newCondition();

    @Override
    public void run() {
        try {
            REENTRANT_LOCK.lock();
            System.out.println("thread is awaiting");
            CONDITION.await();
            System.out.println("thread is going on");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if(REENTRANT_LOCK.isHeldByCurrentThread()){
                REENTRANT_LOCK.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReentrantLockCondition reentrantLockCondition = new ReentrantLockCondition();
        reentrantLockCondition.start();

        Thread.sleep(3000);

        REENTRANT_LOCK.lock();
        CONDITION.signal();
        REENTRANT_LOCK.unlock();
    }

}
